Fix nohup commands not returning immediately with timeout wrapper#329
Merged
Fix nohup commands not returning immediately with timeout wrapper#329
Conversation
When nohup commands with background execution (&) are run through the timeout wrapper in Docker/Kubernetes terminals, the timeout command doesn't return immediately because background processes inherit the shell's stdout/stderr file descriptors. This fix adds proper output redirection (> /dev/null 2>&1) to the gunicorn nohup commands in SWE-Bench setup, ensuring the timeout wrapper returns immediately after the shell exits instead of waiting for the full timeout period. Also adds comprehensive tests for both Docker and Kubernetes terminals to verify nohup commands with proper redirection return immediately. Fixes issue reported in #325 where requests package setup would hang for 300 seconds when starting background gunicorn servers. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
The previous fix using only output redirection was insufficient because in non-TTY mode, the timeout command monitors the entire process group, not just file descriptors. Even with > /dev/null 2>&1, backgrounded processes remain in the same process group as the shell. Using setsid creates a new session, completely detaching the process from timeout's process group. This ensures the timeout-wrapped command returns immediately after the shell exits, even in non-TTY execution contexts like docker exec and kubectl exec. Changes: - Added setsid before nohup gunicorn commands - Updated test names and documentation to reflect setsid usage - Tests verify processes detach properly in non-TTY mode Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
MarcCote
approved these changes
Jan 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix nohup commands not returning immediately with timeout wrapper
Problem
When
nohupcommands with background execution (&) are run through the timeout wrapper in Docker/Kubernetes terminals without TTY, thetimeoutcommand doesn't return immediately. This caused the requests package setup in SWE-Bench to hang for 300 seconds when starting gunicorn servers.Root Cause - Bash Job Control in Non-TTY Mode
timeout 300 /bin/bash -c 'nohup gunicorn ... > /dev/null 2>&1 &'In non-TTY mode:
timeoutcommand monitors the bash shell it launches&, bash's job control tracks the backgrounded processsetsidWhy TTY matters:
Solution - Use Subshell
(...)The fix uses bash subshells
(...)to create a subprocess that exits immediately after backgrounding:Before (insufficient):
After (correct):
Why this works:
(...)creates a subshell that is a separate processsh -cwith equivalent behaviorChanges
debug_gym/gym/envs/swe_bench.py(...)subshell wrapper for 2 nohup gunicorn commandstests/gym/terminals/test_docker.pytests/gym/terminals/test_kubernetes.pyCommit history:
setsidapproach (still had 5+ second delay due to bash job control)sh -capproach (3+ second delay, improved but still not optimal)(...)subshell + warmup commands to exclude startup overheadTesting
Added comprehensive tests verifying
(...)subshell withnohupworks in non-TTY Docker/Kubernetes exec:Test:
test_*_nohup_with_subshell_returns_immediately(nohup ... > /dev/null 2>&1 &)returns in < 2 secondsTest:
test_*_nohup_without_redirection_may_timeoutEach test:
time.time()pgreppkillImpact
sh -corsetsidTechnical Note
This approach uses bash subshells for clean process separation:
(...)- Creates subshell that exits immediately after backgroundingnohup- Ignores SIGHUP signals> /dev/null 2>&1- Closes stdout/stderr file descriptors&- Backgrounds the process within the subshellThe key insight is that subshells
(...)create separate processes that don't participate in the parent shell's job control, allowing immediate return.Related
Fixes timeout issue reported in #325 (screenshot shows the 300s timeout being triggered in Kubernetes exec without TTY)
🤖 Generated with Claude Code